package com.example.demo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.config.B64;
import com.example.demo.config.RSAEncoder;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoginService {

    public Connection getConnection(String IP,String URL){
        Connection connection = Jsoup.connect(IP+URL);
        connection.header("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
        connection.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36");//配置模拟浏览器
        return connection;
    }
    public Connection getConnection(String IP,String URL,Map<String,String> cookies){
        Connection connection = Jsoup.connect(IP+URL).cookies(cookies);
        connection.header("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
        connection.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36");//配置模拟浏览器
        return connection;
    }
    public Response getReponse(Connection connection) throws IOException {
        Response response = connection.execute();
        return response;
    }
    public Response getPostReponse(Connection connection)throws IOException {
        Response response = connection.followRedirects(true).method(Connection.Method.POST).execute();
        return response;
    }
    public Response getDatasReponse(Connection connection,Map<String,String> cookies,Map<String,String> datas)throws IOException {
        Response response = connection.cookies(cookies).method(Connection.Method.POST).data(datas).ignoreContentType(true).execute();
        return response;
    }

    public Document getDocument(Response response){
        Document document = Jsoup.parse(response.body());
        return document;
    }
    public List<Element> getElements(Document document,String cssQuery){
        List<Element> elements = document.select(cssQuery);
        return elements;
    }
    public Map<String, String> getDatas(List<Element> elements,String name,String password){
        Map<String, String> datas = new HashMap<>();
        for (Element e : elements.get(0).getAllElements()) {
            if (e.attr("name").equals("yhm")) {
                e.attr("value", name);
            }
            if (e.attr("name").equals("mm")) {
                e.attr("value", password);
            }
            if (e.attr("name").length() > 0) {
                datas.put(e.attr("name"), e.attr("value"));
            }
        }
        return datas;
    }

    //获取Cookies
    public Map<String,String> getCookies(Response response){
        Map<String,String> cookies = response.cookies();
        return cookies;
    }

    // 获取csrftoken
    public String getCsrftoken(Map<String,String> cookies_innet,
                      String IP){
        try{
            Connection connection = getConnection(IP,"/xtgl/login_slogin.html?time"+new Date().getTime(),cookies_innet);
            Response response = getPostReponse(connection);
            //保存csrftoken
            Document document = getDocument(response);
            String csrftoken = document.getElementById("csrftoken").val();
            return csrftoken;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

    // 获取公钥并加密密码
    public String getRSApublickey(String IP,Map<String,String> cookies,String password) throws Exception{
        Connection connection = getConnection(IP,"/xtgl/login_getPublicKey.html?time="+ new Date().getTime(),cookies);
        Response response = connection.cookies(cookies).ignoreContentType(true).followRedirects(true).execute();

        JSONObject jsonObject = JSON.parseObject(response.body());
        String modulus = jsonObject.getString("modulus");
        String exponent = jsonObject.getString("exponent");
        password = RSAEncoder.RSAEncrypt(password, B64.b64tohex(modulus), B64.b64tohex(exponent));
        password = B64.hex2b64(password);
        return password;
    }
}
